package com.gesturesoft.imultimax.socialize.destroytoday.desktop 
{
	
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.events.Event;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	
	import com.gesturesoft.imultimax.socialize.org.osflash.signals.Signal;

	public class NativeMenuPlus extends NativeMenu
	{

		protected var _data:Object;

		protected var separatorCount:uint;
		
		public function NativeMenuPlus(xml:XML)
		{
			
			build(this, '', xml);
			
		}

		public function get data():Object 
		{
			
			return _data;
			
		}

		public function set data(value:Object):void 
		{
			
			if (value == _data)
				return;

			_data = value;
			
		}
		
		public function checkOnly(item:NativeMenuItem, items:Array = null, ...safe:Array):void
		{
			
			if (!items) items = this.items;
			
			for each (var _item:NativeMenuItem in items)
			{
				
				if (safe && safe.indexOf(_item) != -1) continue;
				
				_item.checked = (_item == item);
				
			}
			
		}
		
		public function checkOnlyLabel(label:String, items:Array = null, ...safe:Array):void
		{
			
			if (!items) items = this.items;
			
			for each (var item:NativeMenuItem in items)
			{
				
				if (safe && safe.indexOf(item) != -1) continue;
				
				item.checked = (item.label == label);
				
			}
			
		}

		protected function build(menu:NativeMenu, menuPath:String, data:XML):void
		{
			
			var i:uint, m:uint;
			var name:String, path:String, nodeName:String;
			var item:NativeMenuItem;
			var keyEquivalentModifiers:Array;

			if (menu == this) 
			{
				
				separatorCount = 0;

				removeAllItems();
				
			}

			for each (var node:XML in data.children()) 
			{
				
				name = node.@name;
				path = menuPath + (menuPath ? ":" : "") + name;
				nodeName = node.name();

				if (nodeName == "item") 
				{
					
					item = menu.addItem(new NativeMenuItem(node.@label));

					if (String(node.@keyEquivalent))
						item.keyEquivalent = node.@keyEquivalent;

					if (String(node.@keyEquivalentModifiers)) 
					{
						
						keyEquivalentModifiers = String(node.@keyEquivalentModifiers).split(",");

						m = keyEquivalentModifiers.length;

						for (i = 0; i < m; ++i) 
						{
							
							switch (keyEquivalentModifiers[i])
							{
								
								case "command":
									keyEquivalentModifiers[i] = Keyboard.COMMAND;
									break;
								case "control":
									keyEquivalentModifiers[i] = Keyboard.CONTROL;
									break;
								case "option":
								case "alt":
								case "alternate":
									keyEquivalentModifiers[i] = Keyboard.ALTERNATE;
									break;
								case "shift":
									keyEquivalentModifiers[i] = Keyboard.SHIFT;
									break;
								
							}
							
						}
						
					}
					else
					{
						
						keyEquivalentModifiers = [];
						
					}

					item.keyEquivalentModifiers = keyEquivalentModifiers;

					item.checked = String(node.@checked) == "true";
					item.enabled = String(node.@enabled) != "false";
					
				} 
				else if (nodeName == "menu") 
				{
					
					item = menu.addItem(new NativeMenuItem(node.@label));

					item.submenu = new NativeMenu();

					build(item.submenu, path, node);
					
				} 
				else if (nodeName == "separator") 
				{
					
					item = menu.addItem(new NativeMenuItem("", true));

					if (!name)
						name = "separator" + separatorCount++;
					
				}

				item.name = name;
				
				if (hasOwnProperty(name)) this[name] = item;
				
			}
			
		}
		
	}
	
}